package com.balabala.gym_system.controller.admin;

import com.balabala.gym_system.config.AdminThreadLocal;
import com.balabala.gym_system.pojo.dto.MenuDto;
import com.balabala.gym_system.pojo.dto.RoutersMenuDto;
import com.balabala.gym_system.pojo.entity.Menu;
import com.balabala.gym_system.service.MenuService;
import com.balabala.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Author:zl
 * Date:2023/5/1 15:26
 */

@Api(tags = "菜单权限接口")
@RestController
@RequestMapping("/admin/menu")
@CrossOrigin
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 添加菜单
     *
     * @param menu
     */
    @ApiOperation(value = "添加菜单", notes = "添加菜单", httpMethod = "POST")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result save(@RequestBody Menu menu) {
        menuService.save(menu);
        return Result.ok();
    }

    /**
     * 获取菜单树
     */
    @ApiOperation(value = "获取菜单树", notes = "获取菜单树", httpMethod = "GET")
    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    public Result getMenuTree() {
        //超级管理员查询所有菜单
        System.out.println(AdminThreadLocal.get().getUsername());
        Long adminId = AdminThreadLocal.get().getUsername().equals("admin")?0L: AdminThreadLocal.get().getId() ;
        List<Menu> menuList = menuService.selectMenuTree(adminId);
        return Result.ok(menuList);
    }


    /**
     * 获取所有菜单
     *
     * @return
     */
    @ApiOperation(value = "获取所有菜单", notes = "获取所有菜单", httpMethod = "GET")
    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public Result getMenus() {
        //设置为0时，service不会走条件查询，会查询所有菜单
        List<Menu> menuList = menuService.selectMenuTree(0L);
        return Result.ok(menuList);
    }


    /**
     * 修改菜单
     *
     * @param menuDto
     * @return
     */
    @ApiOperation(value = "修改菜单", notes = "修改菜单", httpMethod = "POST")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateMenu(@RequestBody MenuDto menuDto) {
        menuService.updateMenuById(menuDto);
        return Result.ok();
    }

    /**
     * 根据id删除菜单
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除菜单", notes = "根据id删除菜单", httpMethod = "GET")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result deleteMenu(@PathVariable("id") Long id) {
        menuService.removeMenuById(id);
        return Result.ok();
    }

    /**
     * 获取路由
     *
     * @return
     */
    @ApiOperation(value = "获取路由", notes = "获取路由", httpMethod = "GET")
    @RequestMapping(value = "getRouters", method = RequestMethod.GET)
    public Result getRouters() {
        //超级管理员获取所有路由
        Long adminId = AdminThreadLocal.get().getUsername().equals("admin") ? 0L : AdminThreadLocal.get().getId();
        List<Menu> menuList = menuService.selectMenuTree(adminId);
        return Result.ok(RoutersMenuDto.buildMenus(menuList));
    }

    /**
     * 根据角色ID获取其所有权限ID
     *
     * @param id role id
     */
    @ApiOperation(value = "根据角色ID获取其所有权限ID", notes = "根据角色ID获取其所有权限ID", httpMethod = "GET")
    @RequestMapping(value = "getRoleHasMenu", method = RequestMethod.GET)
    public Result getRoleHasMenu(Long id) {
        List<Long> allMenu = menuService.getMenuByRoleId(id);
        return Result.ok(allMenu);
    }
}